$(function () {
    let layer = layui.layer;
    let form = layui.form;
    let laypage = layui.laypage;
    // 定义一个存储数据的对象
    const q = {
        pagenum: 1,//页码值
        pagesize: 2,//每页的显示条数
        cate_id: '',//文章id
        state: ''//文章发布状态
    };

    //过滤器
    template.defaults.imports.dataFormat = function (data) {
        const dt = new Date(data);
        let y = dt.getFullYear();
        let m = (dt.getMonth() + 1).toString().padStart(2, '0');
        let d = dt.getDate().toString().padStart(2, '0');
        let hh = dt.getHours().toString().padStart(2, '0');
        let mm = dt.getMinutes().toString().padStart(2, '0');
        let ss = dt.getSeconds().toString().padStart(2, '0');
        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`

    }

    //获取文章列表数据方法
    function initTable() {
        $.ajax({
            method: 'get',
            url: '/my/article/list',
            data: q,
            success(res) {
                console.log(res);
                if (res.status !== 0) {
                    return layer.msg(res.message, { icon: 2 })
                }
                layer.msg(res.message, { icon: 1 })
                let htmlStr = template('tpl-table', res);
                $('tbody').html(htmlStr)
                renderPage(res.total)
            }
        })
    }
    initTable()

    //获取文章分类
    function initCate() {
        $.ajax({
            method: 'get',
            url: '/my/article/cates',
            success(res) {
                if (res.status !== 0) {
                    return layer.msg(res.message, { icon: 2 })
                }
                let htmlStr = template('tpl-cate', res)
                $('[name=cate_id]').html(htmlStr);
                form.render()
            }
        })
    }
    initCate()

    //为筛选表单绑定submit事件
    $("#form-search").on('submit', function (e) {
        e.preventDefault();
        q.cate_id = $('[name=cate_id]').val();
        q.state = $('[name=state]').val();
        initTable()

    })

    //渲染分页函数
    function renderPage(total) {
        laypage.render({
            elem: 'pageBox',
            count: total,
            limit: q.pagesize,
            curr: q.pagenum,
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 3, 5, 10],
            jump: function (obj, first) {
                if (!first) {
                    q.pagenum = obj.curr
                    q.pagesize = obj.limit
                    initTable()
                }
            },
        })
    }

    //删除
    $('tbody').on('click', '.btn-delete', function () {
        let id = $(this).attr('data-id')
        layer.confirm('能确定要删除吗?', { icon: 3, title: '提示' }, function (index) {
            $.ajax({
                method: 'GET',
                url: '/my/article/delete/' + id,
                success(res) {
                    if (res.status !== 0) {
                        return layer.msg(res.message, { icon: 2 })
                    }
                    layer.msg(res.message, { icon: 1 })
                    layer.close(index);
                    if ($('.btn-delete').length <= 1) {
                        q.pagenum = q.pagenum > 1 ? q.pagenum - 1 : 1;
                    }
                    initTable()
                }
            })

        });
    })

    //编辑
})